/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.database.pojo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import org.aergia.vinny.core.enums.DayOfWeek;
import org.w3c.dom.Element;

/**
 * @author fred
 */
@Entity
public class OperatingHours implements DatabasePojo {

	private boolean active;
	private int dayOfWeek;
	private int endHour;
	private int id;
	private Location location;
	private int startHour;

	/**
	 * 
	 */
	public OperatingHours() {
		// empty
	}

	/**
	 * @param saturday
	 * @param start
	 * @param end
	 */
	public OperatingHours(final DayOfWeek day, final int start, final int end) {
		this.dayOfWeek = day.getValue();
		this.startHour = start;
		this.endHour = end;
		this.active = true;
	}

	@Override
	public void attachAttributes(final Element element) {
		element.setAttribute("id", Integer.toString(this.getId()));
		element.setAttribute("active", Boolean.toString(this.isActive()));
		element.setAttribute("dayOfWeek", this.getDayOfWeek().toString());
		element.setAttribute("endHour", Integer.toString(this.getEndHour()));
		element.setAttribute("location", Integer.toString(this.getLocation().getId()));
		element.setAttribute("startHour", Integer.toString(this.getStartHour()));
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof OperatingHours)) {
			return false;
		}
		final OperatingHours other = (OperatingHours) obj;
		if (this.getDayOfWeek() != other.getDayOfWeek()) {
			return false;
		}
		if (this.getLocation() == null) {
			if (other.getLocation() != null) {
				return false;
			}
		} else if (!this.getLocation().equals(other.getLocation())) {
			return false;
		}
		if (this.getDayOfWeek() == null) {
			if (other.getDayOfWeek() != null) {
				return false;
			}
		} else if (!this.getDayOfWeek().equals(other.getDayOfWeek())) {
			return false;
		}
		return true;
	}

	public int getDay() {
		return this.dayOfWeek;
	}

	public DayOfWeek getDayOfWeek() {
		return DayOfWeek.valueOf(this.dayOfWeek);
	}

	/**
	 * @return the endHour
	 */
	public int getEndHour() {
		return this.endHour;
	}

	@Override
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return this.id;
	}

	/**
	 * @return the location
	 */
	@ManyToOne
	public Location getLocation() {
		return this.location;
	}

	/**
	 * @return the startHour
	 */
	public int getStartHour() {
		return this.startHour;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (prime * result) + (this.getDayOfWeek() == null ? 0 : this.getDayOfWeek().getValue());
		result = (prime * result) + (this.getLocation() == null ? 0 : this.getLocation().hashCode());
		return result;
	}

	@Override
	public boolean isActive() {
		return this.active;
	}

	/**
	 * @param active
	 *            the active to set
	 */
	public void setActive(final boolean active) {
		this.active = active;
	}

	public void setDay(final int day) {
		this.dayOfWeek = day;
	}

	/**
	 * @param dayOfWeek
	 *            the dayOfWeek to set
	 */
	public void setDayOfWeek(final int dayOfWeek) {
		this.dayOfWeek = dayOfWeek;
	}

	/**
	 * @param endHour
	 *            the endHour to set
	 */
	public void setEndHour(final int endHour) {
		this.endHour = endHour;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(final int id) {
		this.id = id;
	}

	/**
	 * @param location
	 *            the location to set
	 */
	public void setLocation(final Location location) {
		this.location = location;
	}

	/**
	 * @param startHour
	 *            the startHour to set
	 */
	public void setStartHour(final int startHour) {
		this.startHour = startHour;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "OperatingHour [id=" + this.getId() + ", location=" + this.getLocation() + ", startHour=" + this.getStartHour() + ", endHour="
				+ this.getEndHour() + ", dayOfWeek=" + this.getDayOfWeek() + "]";
	}
}
